package com.wangjiacai.util;


import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/*
 * @auther: wangjiacai
 *
 * @create: 2018-08-07 17:18:26
 */
public class BeanUtil {

    public static PropertyDescriptor[] getPropertyDescriptors(Object object){
        Class obClass=object.getClass();
        Field[] fields=obClass.getDeclaredFields();
        PropertyDescriptor[] pds=new PropertyDescriptor[fields.length];
        for(int i=0;i< fields.length;i++){
            try{
                pds[i]= new PropertyDescriptor(fields[i].getName(),obClass);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return pds;
    }

    public static Object getPropertyValue(Object object,String property){
        Class obClass=object.getClass();
        PropertyDescriptor pds;
        try {
            pds = new PropertyDescriptor(property,obClass);
            Method read=pds.getReadMethod();
            return read.invoke(object);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

}
